package com.ztjava.remind.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.ztjava.core.Constant;
import com.ztjava.core.base.BaseService;
import com.ztjava.core.bean.remind.RemindMsgBean;
import com.ztjava.core.tool.ViewTool;
import com.ztjava.remind.task.MpTask;
/***
 * 系统内消息提醒
 * @author Administrator
 *
 */
public class RemindService extends BaseService{
	public static ExecutorService mpPool = Executors.newSingleThreadExecutor();
	
	public void sendMp(Integer user_id,String code,String url,String[]... dataArr){
		
		Cache cache = Redis.use();
		String openid = cache.hget(Constant.R_PRE+"AUTH:THIRD:WEIXIN", user_id);
		String template_id = ViewTool.param(code);
		if(openid!=null&&!template_id.equals("")&&dataArr!=null&&dataArr.length>0){
			JSONObject data = new JSONObject();
			for(String[] item:dataArr){
				JSONObject vo = new JSONObject();
				vo.put("value", item[1]);
				if(item.length>2){
					vo.put("color", item[2]);
				}
				data.put(item[0], vo);
			}
			mpPool.execute(new MpTask(openid, template_id, url, data));
		}
	}
	
	//发布消息
	public boolean addMsg(RemindMsgBean remindMsgBean){
		Cache cache = Redis.use();
		if(remindMsgBean.getSeconds()!=null&&remindMsgBean.getSeconds()>0l){
			String key = Constant.R_PRE+"REMIND:MSG:REPEAT:"
					+remindMsgBean.getPlugin()	+":"
					+remindMsgBean.getComponent()+":"
					+remindMsgBean.getModel()+":"
					+remindMsgBean.getObject_id()+":"
					+remindMsgBean.getFrom_user_id();
			if(cache.exists(key)){
				return false;
			}
			cache.setex(key,remindMsgBean.getSeconds(), true);
		}

		cache.lpush(Constant.R_PRE+"REMIND:MSG:LIST:"+remindMsgBean.getPlugin()+":"+remindMsgBean.getTo_user_id(), remindMsgBean.getId());
		cache.lpush(Constant.R_PRE+"REMIND:MSG:LIST:"+remindMsgBean.getPlugin()+":"+remindMsgBean.getTo_user_id()+":"+remindMsgBean.getComponent(), remindMsgBean.getId());
		cache.hset(Constant.R_PRE+"REMIND:MSG:DATA:"+remindMsgBean.getPlugin()+":"+remindMsgBean.getTo_user_id(), remindMsgBean.getId(), remindMsgBean);
		return true;
	}
	
	//分页获取消息列表
	public List<RemindMsgBean> getMsgList(String plugin,Integer to_user_id,Integer page,Integer size){
		return getMsgList(plugin, null,to_user_id, page, size);
	}
	@SuppressWarnings("unchecked")
	public List<RemindMsgBean> getMsgList(String plugin,String component,Integer to_user_id,Integer page,Integer size){
		Cache cache = Redis.use();
		if(component!=null&&!component.equals("")){
			component = ":"+component;
		}else {
			component = "";
		}
		Long len = cache.llen(Constant.R_PRE+"REMIND:MSG:LIST:"+plugin+":"+to_user_id+component);
		List<RemindMsgBean> list = new ArrayList<>();
		if(len==0l){
			return list;
		}
		Integer start = (page-1)*size;
		Integer end = start +size -1;
		List<String> dataList = cache.lrange(Constant.R_PRE+"REMIND:MSG:LIST:"+plugin+":"+to_user_id+component, start, end);
		for(int i = 0;i<dataList.size();i++){
			list.add((RemindMsgBean)cache.hget(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id, dataList.get(i)));
		}
		return list;
	}
	//消息已读
	public void readMsg(String plugin,Integer to_user_id,String id){
		Cache cache = Redis.use();
		RemindMsgBean remindMsgBean = cache.hget(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id,id);
		if(remindMsgBean!=null){
			remindMsgBean.setIs_read("T");
			cache.hset(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id,id, remindMsgBean);
		}
	}
	//全部消息已读
	public void readAllMsg(String plugin,Integer to_user_id){
		Cache cache = Redis.use();
		if(!cache.exists(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id)){
			return;
		}
		for( Object key:cache.hkeys(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id)){
			RemindMsgBean remindMsgBean = cache.hget(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id,key);
			if(remindMsgBean!=null){
				remindMsgBean.setIs_read("T");
				cache.hset(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id,key, remindMsgBean);
			}
		}
	}
	//消息清空
	public void deleteAll(String plugin,Integer to_user_id){
		Cache cache = Redis.use();
		for(String key:cache.keys(Constant.R_PRE+"REMIND:MSG:LIST:"+plugin+":"+to_user_id+"*")){
			cache.del(key);
		}
		cache.del(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id);
	}
	//删除指定消息
	public void deleteMsg(String plugin,Integer to_user_id,String id){
		Cache cache = Redis.use();
		for(String key:cache.keys(Constant.R_PRE+"REMIND:MSG:LIST:"+plugin+":"+to_user_id+"*")){
			System.out.println(key);
			cache.lrem(key, 1, id);
		}
		cache.hdel(Constant.R_PRE+"REMIND:MSG:DATA:"+plugin+":"+to_user_id,id);
	}
}
